/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Images;

import Interface.IClient;

import Interface.IMain;
import StaticClass.ArrSocket;
import Thread.SendComputerName;
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.ScrollPane;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

/**
 *
 * @author Oho
 */
public class RMImages extends Thread {

    Socket sk;
    ObjectInputStream inp;
    IClient pn;
    Rectangle rt;
    ImageIcon icon;
    JPanel scr;
    String name = "";

    public ImageIcon getIcon() {
        return icon;
    }
    //int i=0;

    public RMImages(Socket sk, JPanel scr) {
        this.sk = sk;
//        this.pn = pn;
        this.scr = scr;


    }

    public void ThreadRead() throws IOException {
        try {


            icon = (ImageIcon) inp.readObject();
            Image img = icon.getImage();

            img = img.getScaledInstance(210, 220, Image.SCALE_FAST);
            pn.jLabel1.setIcon(new ImageIcon(img));
            pn.jLabel1.setBounds(0, 0, 210, 200);
            img.flush();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RMImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void run() {
        try {
            inp = new ObjectInputStream(sk.getInputStream());
            ObjectOutputStream out = new ObjectOutputStream(sk.getOutputStream());

            name = SendComputerName.SendName(inp, out);
            pn = new IClient(this, ArrSocket.ArrClientIMG.size(), name);
            //   pn.txtNameMachine.setText("Day la ten may");
            
            
            pn.setBounds((ArrSocket.ArrClientIMG.size() % 5) * (scr.getX() + 250) + 20, ((ArrSocket.ArrClientIMG.size()) / 5) * (scr.getY() + 250) + 20, 200, 230);
           
            ArrSocket.ArrClientIMG.add(pn);
            scr.add(pn);

            rt = (Rectangle) inp.readObject();

            while (true) {
                try {
                    ThreadRead();
                } catch (IOException ex) {
                    Logger.getLogger(RMImages.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(RMImages.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RMImages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
